/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */


package cn.edu.thu.log.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
/**
 * 
 * @author dongzihe
 *
 */
public final class Format {
	

	private Format() {
		super();
	}
	/**
	 * setup timestamp in Date format
	 * 
	 * @param timeString
	 *            String of timeStamp
	 * @return Date format as timeStamp
	 */
	public static Date stringToTimeStamp(String timeString) {
		Date timeStamp = new Date();
		if (timeString != null) {
			try {
				timeString = timeString.trim();
				timeStamp
						.setYear(Integer.parseInt(timeString.substring(0, 4)) - 1900);
				timeStamp
						.setMonth(Integer.parseInt(timeString.substring(4, 6)) - 1);
				timeStamp.setDate(Integer.parseInt(timeString.substring(6, 8)));
				timeStamp
						.setHours(Integer.parseInt(timeString.substring(8, 10)));
				timeStamp.setMinutes(Integer.parseInt(timeString.substring(10,
						12)));
				timeStamp.setSeconds(Integer.parseInt(timeString.substring(12,
						14)));
			} catch (Exception e) {
				 System.out.print("");

			}
		}
		return timeStamp;
	}
/**
 * 
 * @param timeString parameter
 * @return
 */
	public static Date dateStringToDate(String timeString) {
		// System.out.println("before:" + timeString);
		// "2012-03-01T09:04:31.282+08:00";
		Date date = new Date();

		if (timeString != null) {
			try {
				String[] timeStrings = timeString.split("T");
				String datePart = timeStrings[0];
				String[] dateStrings = datePart.split("-");
				String yearString = dateStrings[0];
				String monthString = dateStrings[1];
				String dayString = dateStrings[2];

				String timePart = timeStrings[1];
				int index = timePart.indexOf("+");			

				String time1 = timePart.substring(0, index);
				index = time1.indexOf(".");
				String time;
				if (index != -1) {
					time = time1.substring(0, index);
				} else {
					time = time1;

				}
				String miniString = time1.substring(index + 1);
				String[] time2 = time.split(":");
				String hourString = time2[0];
				String minuteString = time2[1];
				String secondString = time2[2];

				date = new Date();
				date.setYear(Integer.parseInt(yearString) - 1900);
				date.setMonth(Integer.parseInt(monthString) - 1);
				date.setDate(Integer.parseInt(dayString));
				date.setHours(Integer.parseInt(hourString));
				date.setMinutes(Integer.parseInt(minuteString));
				date.setSeconds(Integer.parseInt(secondString));

			} catch (Exception e) {
				System.out.print("\ntime format wrong as:" + timeString);

			}

		}
		// System.out.println("after:"+date);
		return date;
	}

	/**
	 * split content by Tokenizer to List
	 * 
	 * @param tokenizer Tokenizer
	 * @param content content
	 * @return a list of content fields
	 */
	public static ArrayList<String> splitContentToList(String tokenizer,
			String content) {

		// Split logHeadContent by logHeadTokenizer
		int len = content.length();
		int pos = 0;
		ArrayList<String> alParams = new ArrayList<String>();
		int nl = tokenizer.length();
		int oldPos = 0;
		while (pos < len) {
			int idx = content.indexOf(tokenizer, pos);
			if (idx != -1) {
				pos = idx + nl;

				if (idx == 0) {
					alParams.add("");
					oldPos = pos;
				} else if (content.charAt(idx - 1) != '\\') {
					alParams.add(content.substring(oldPos, idx));
					oldPos = pos;
				}

				if (pos == len) {
					alParams.add("");
				}
			} else {
				alParams.add(content.substring(pos + nl));
				pos = len;
			}
		}

		return alParams;
	}

	/**
	 * split content by Tokenizer to List
	 * 
	 * @param tokenizer Tokenizer
	 * @param content content
	 * @return a list of content fields
	 */
	public static Hashtable<String, String> splitContentToMap(String tokenizer,
			String content) {
		// System.out.println("content:"+content);
		// Split logHeadContent by logHeadTokenizer
		int len = content.length();
		int pos = 0;
		Hashtable<String, String> alParamMap = new Hashtable();
		// ArrayList<String> alParams = new ArrayList<String>();
		int nl = tokenizer.length();
		int oldPos = 0;
		while (pos < len) {
			int idx = content.indexOf(tokenizer, pos);
			if (idx != -1) {
				pos = idx + nl;

				if (idx == 0) {
					alParamMap.put("", "");
					// alParams.add("");
					oldPos = pos;
				} else if (content.charAt(idx - 1) != '\\') {
					String tempField = content.substring(oldPos, idx);
					String[] tempFields;
					String abbre = "";
					String value = "";
					if (tempField.contains(":")) {

						tempFields = splitLogBodyField(tempField);
						abbre = tempFields[0];
						if (tempFields.length > 1) {
							value = tempFields[1];
						}
					}
					alParamMap.put(abbre, value);
					// alParams.add(content.substring(oldPos, idx));
					oldPos = pos;
				}

				if (pos == len) {
					String tempField = content.substring(oldPos - 1, pos);
					String[] tempFields;
					String abbre = "";
					String value = "";
					if (tempField.contains(":")) {
						tempFields = splitLogBodyField(tempField);
						abbre = tempFields[0];
						if (tempFields.length > 1) {
							value = tempFields[1];
						}
					}
					alParamMap.put(abbre, value);
					// alParamMap.put("", "");

				}
			} else {
				int lastIndex = content.lastIndexOf(tokenizer);
				String tempField = content.substring(lastIndex + 1);
				String[] tempFields;
				String abbre = "";
				String value = "";
				if (tempField.contains(":")) {

					tempFields = splitLogBodyField(tempField);
					if (tempFields.length != 0) {
						abbre = tempFields[0];
					}
					if (tempFields.length > 1) {
						value = tempFields[1];
					}
				}
				alParamMap.put(abbre, value);
				// alParams.add(content.substring(pos + nl));
				pos = len;
			}
		}
		// Enumeration enu =alParamMap.keys();
		// while(enu.hasMoreElements()){
		// String key= (String) enu.nextElement();
		// String value=alParamMap.get(key);

		// }

		return alParamMap;
	}

	private static String[] splitLogBodyField(String logBodyField) {
		String[] logBodyDetail = new String[2];
		int tempIndex = logBodyField.indexOf(":");
		// System.out.println("tempIndex:" + tempIndex);
		logBodyDetail[0] = logBodyField.substring(0, tempIndex);
		// System.out.println("logBodyDetail[0]:"+logBodyDetail[0]);
		logBodyDetail[1] = logBodyField.substring(tempIndex + 1);
		// System.out.println("logBodyDetail[1]:"+logBodyDetail[1]);
		return logBodyDetail;
	}
}
